//
//  MoreViewController.m
//  Follow Me
//
//  Created by apple on 2017/4/10.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "MoreViewController.h"
#import "RouteCell.h"
#import "DestinationCell.h"
#import "Destinations+CoreDataClass.h"
#import "Routes+CoreDataClass.h"
#import "Users+CoreDataClass.h"
#import "Comments+CoreDataClass.h"
#import "DetailOfRouteViewController.h"
#import "DetailOfDestinationViewController.h"
#import "MainModel.h"
extern int choo;
@interface MoreViewController ()
@property (nonatomic,strong)NSArray *RouteCells;
@property (nonatomic,strong)NSArray *DestinationCells;
@property (nonatomic)Destinations *destination;
@property (nonatomic)Users *user;
@property (nonatomic,strong) DestinationCell *Dcell;
@property (nonatomic,strong) RouteCell *Rcell;
@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSArray *destinationInfo = [Destinations findAllSortedBy:@"destinationName" ascending:YES];
//    NSArray *des1 = [destinationInfo subarrayWithRange:NSMakeRange(0,3)];
//    NSArray *des2 = [destinationInfo subarrayWithRange:NSMakeRange(3,3)];
    NSArray *routeInfo = [Routes findAllSortedBy:@"routeName" ascending:YES];
    self.RouteCells = routeInfo;
//    self.DestinationCells = des1;
    self.DestinationCells = destinationInfo;
    
//    NSLog(@"%@",_DestinationCells);
     [self.tableView registerNib:[UINib nibWithNibName:@"DestinationCell" bundle:nil] forCellReuseIdentifier:@"DestinationCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"RouteCell" bundle:nil] forCellReuseIdentifier:@"RouteCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.navigationController.navigationBar.frame = CGRectMake(0, 20, 375, 40);
    
    if (choo == 0)
    {
        self.ChooseRouteOrDestination.selectedSegmentIndex = 0;
//        [self.tableView registerNib:[UINib nibWithNibName:@"RouteCell" bundle:nil] forCellReuseIdentifier:@"RouteCell"];
    }
    else{
        self.ChooseRouteOrDestination.selectedSegmentIndex = 1;
//        [self.tableView registerNib:[UINib nibWithNibName:@"DestinationCell" bundle:nil] forCellReuseIdentifier:@"DestinationCell"];
    }
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (choo == 0){
        return [self.RouteCells count];
    }else{
        return [self.DestinationCells count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (choo == 0){
        
        RouteCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"RouteCell"forIndexPath:indexPath];
        [cell ConfigRouteCell:[self.RouteCells objectAtIndex:indexPath.row]];
        if (cell == nil){
            cell=[[[NSBundle mainBundle]loadNibNamed:@"RouteCell" owner:(self) options:nil]lastObject];
        }
        
        return cell;
        
    }else{
        DestinationCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"DestinationCell"forIndexPath:indexPath];
        if (cell == nil){
            cell=[[[NSBundle mainBundle]loadNibNamed:@"DestinationCell" owner:(self) options:nil]lastObject];
        }
        [cell ConfigDestinationCell:[self.DestinationCells objectAtIndex:indexPath.row]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (choo == 0){
    [MainModel model].routeMsg = self.RouteCells[indexPath.row];
    
    DetailOfRouteViewController *detailOfRouteVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailOfRoute"];
    [self.navigationController pushViewController:detailOfRouteVC animated:YES];
    }
    else
    {
        [MainModel model].destinatonMsg = self.DestinationCells[indexPath.row];
        DetailOfDestinationViewController *detailOfDestinationVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailOfDestination"];
        [self.navigationController pushViewController:detailOfDestinationVC animated:YES];
    }
}

//- (void)ConfigRouteCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)indexPath{
//    Routes *route1 = self.RouteCells[indexPath.row];
//    self.Rcell.RouteCellUserName.text = route1.user.userName;
//    //nsdate转换成nsstring
//    NSDate *date = [NSDate date];
//    date = route1.routeDate;
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
//    self.Rcell.RouteCellDate.text = [dateFormatter stringFromDate:date];
//    
//    self.Rcell.RouteCellRouteDescription.text =route1.routeDescription;
//    self.Rcell.RouteCellUserLogo.image = [UIImage imageWithData:route1.user.userPicture];
//    self.Rcell.RouteCellUIImage.image = [UIImage imageWithData:route1.introducePicture];
//    
//}

//- (void)ConfigDestinationCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)indexPath{
//    
//    NSArray *destinationInfo = [Destinations findAllSortedBy:@"destinationName" ascending:YES];
//    NSArray *des1 = [destinationInfo subarrayWithRange:NSMakeRange(0,3)];
//    NSArray *des2 = [destinationInfo subarrayWithRange:NSMakeRange(3,3)];
//    
//    Destinations *destination1 = des1[indexPath.row];
//    Destinations *destination2 = des2[indexPath.row];
//    
//    UIImage *image1 = [UIImage imageWithData:destination1.backgroundPIC];
//    UIImage *image2 = [UIImage imageWithData:destination2.backgroundPIC];
//    [self.Dcell.DestinationCellUIImage1 setImage:image1 forState:UIControlStateNormal];
//    [self.Dcell.DestinationCellUIImage2 setImage:image2 forState:UIControlStateNormal];
//    [self.Dcell.DestinationCellUIImage1 setTitle:destination1.destinationName forState:UIControlStateNormal];
//    [self.Dcell.DestinationCellUIImage2 setTitle:destination2.destinationName forState:UIControlStateNormal];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (choo == 0){
        return 270.0f;
    }
    return 234.0f;
}





- (IBAction)chooseRD:(id)sender {
    if (self.ChooseRouteOrDestination.selectedSegmentIndex == 0){
        choo = 0;
        [self.tableView reloadData];
        NSLog(@"选了路线");
    }else{
        choo = 1;
        [self.tableView reloadData];
        NSLog(@"选了目的地");
    }
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)dealloc
{
    
}

@end
